package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.StageLogDao;
import com.xbongbong.paas.domain.entity.StageLogEntity;
import com.xbongbong.paas.model.StageLogModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: yingjianfeng
 * @Date: 2021/11/16 13:31
 * @Description:
 */
@Service("stageLogModel")
public class StageLogModelImpl implements StageLogModel {

    @Resource
    private StageLogDao stageLogDao;

    @Override
    public StageLogEntity getByKey(Long key, String corpid) {
        return stageLogDao.getByKey(key, corpid);
    }

    @Override
    public Integer insert(StageLogEntity entity) throws XbbException {
        return stageLogDao.insert(entity);
    }

    @Override
    public Integer update(StageLogEntity entity) throws XbbException {
        return stageLogDao.update(entity);
    }

    @Override
    public List<StageLogEntity> findEntitys(Map<String, Object> param) {
        return stageLogDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return stageLogDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer deleteBatch(List<StageLogEntity> stageLogEntityList, String corpid) {
        return stageLogDao.deleteBatch(stageLogEntityList, corpid);
    }

    @Override
    public void save(StageLogEntity stageLogEntity) {
        if (stageLogEntity.getId() == null) {
            stageLogDao.insert(stageLogEntity);
        } else {
            long now = DateTimeUtil.getInt();
            stageLogEntity.setUpdateTime(now);
            stageLogDao.update(stageLogEntity);
        }
    }

    @Override
    public StageLogEntity getNowStageLog(Long dataId, String corpid, Long formId) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("formId",formId);
        map.put("dataId",dataId);
        map.put("isNow",1);
        map.put("del",0);
        map.put("start",0);
        map.put("pageNum",1);
        List<StageLogEntity> stageLogEntityList = stageLogDao.findEntitys(map);
        if (stageLogEntityList.isEmpty()){
            return null;
        }else {
            return stageLogEntityList.get(0);
        }
    }

    @Override
    public StageLogEntity getIsNowStage(String corpid, Long formId, Long dataId) {
        return stageLogDao.getIsNowStage(corpid, formId, dataId);
    }

    @Override
    public Long getAvgStayTime(String corpid, Long stageProcessId, Long stageId) {
        return stageLogDao.getAvgStayTime(corpid, stageProcessId, stageId);
    }

    @Override
    public List<Long> findUsedStageProcessId(String corpid, Long formId, List<Long> stageProcessIdIn) {
        return stageLogDao.findUsedStageProcessId(corpid, formId, stageProcessIdIn);
    }
}
